<html class="no-js" lang="en">

<head>
   <meta charset="utf-8" />

   <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" />
   <meta name="format-detection" content="telephone=no" />

   
   <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" /> 
	<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
	<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="http://code.google.com/apis/gears/gears_init.js"></script> 
	<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&libraries=geometry"></script>

<script type="text/javascript"> 

var map;
var geocoder;  
var directionDisplay;
var directionsService = new google.maps.DirectionsService();

function init() {

<?php
include 'config.php';

$comp_array = $_POST['check_comp'];
$n = count($comp_array);

$conn = mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db("mweb_survey");

echo "var des = new Array();\n";
echo "var waypts = [];\n";
for ($i = 0; $i < $n; $i++) {
	$cid = $comp_array[$i];
	$sql = "SELECT * FROM company WHERE CompID = '$cid'";
	$result = mysql_query($sql, $conn) or die(mysql_error());
	$row = mysql_fetch_assoc($result);
	$comp_name = $row['CompName'];
	$address = $row['StreetAddr'] . ", " . $row['City'] . ", " . $row['State'];

echo <<< HERE
des[$i] = "$address";\n

waypts.push({
	location: "$address",
	stopover: true
});\n
HERE;
}

// print_r($comp_array);
$start = $_POST['start'];

if ($start == "") {
echo <<< HERE
	geocoder = new google.maps.Geocoder();
	var distance = new Array();
	var temp = new Array();
	var max = 0;
	
	navigator.geolocation.getCurrentPosition(function(position) {
		var start = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);		
		
		var myOptions = {
			zoom: 15,
			center: start,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
					
		directionsDisplay = new google.maps.DirectionsRenderer();
		directionsDisplay.setMap(map);
		
		// for (var i = 0; i < $n; i++) {
			// geocoder.geocode( { 'address': des[i]}, function(results, status) {
				// if (status == google.maps.GeocoderStatus.OK) {
					// alert(des[i]);
					// distance[i] = google.maps.geometry.spherical.computeDistanceBetween(start, results[0].geometry.location);
					// distance[i] = distance[i]/1000*1.54;
					// distance[i] = distance[i].toFixed(1);
					// temp[i] = distance[i];
					
					// if (temp[i] > max) {
						// max = temp[i];
						// final = des[i];
					// }
				// }
			// });
		// }
		
		var request = {
			origin: start,
			destination: start,
			waypoints: waypts,
			optimizeWaypoints: true,
			travelMode: google.maps.DirectionsTravelMode.DRIVING
		};
		
		directionsService.route(request, function(response, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				directionsDisplay.setDirections(response);
				
				var route = response.routes[0];
				var panel = document.getElementById("directions_panel");
				panel.innerHTML = "";
				
				for (var j = 0; j < route.legs.length; j++) {
					var routeSegment = j + 1;
					panel.innerHTML += "<b>Route Segment: " + routeSegment + "</b><br />";
					panel.innerHTML += route.legs[j].start_address + "<br/> to <br/>";
					panel.innerHTML += route.legs[j].end_address + "<br />";
					panel.innerHTML += route.legs[j].distance.text + "<br /><br />";
				}
			}
		});
		

	});
HERE;
}
?>

}

</script>

<body onload="init()">
	<div id="directions_panel"></div>
	<div id="map_canvas"></div>
</body>
</html>